package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeletePermissionOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.DeleteRolePermissionOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PermissionDTO;
import com.cskaoyan.wordmemorize.request.PermissionCommand;
import com.cskaoyan.wordmemorize.service.RolePermissionService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/permission")
public class PermissionController {

    @Resource
    RolePermissionService rolePermissionService;

    //添加账号权限
    @PostMapping("")
    @SaAdminCheckPermission("admin:permission:add")
    public Result<String> savePermissions(@RequestBody PermissionCommand command) {
        rolePermissionService.savePermissions(command);
        return Result.ok("添加账号权限成功");
    }

    //修改账号权限
    @PutMapping("")
    @SaAdminCheckPermission("admin:permission:update")
    public Result<String> updatePermission(@RequestBody PermissionCommand command) {
        rolePermissionService.updatePermission(command);
        return Result.ok("修改账号权限成功");
    }

    //账号权限列表
    @GetMapping("list")
    public Result<List<PermissionDTO>> getPermissions() {
        List<PermissionDTO> permissionDTOS = rolePermissionService.getPermissions();
        return Result.ok(permissionDTOS);
    }

    //删除指定账号权限
    @DeleteMapping("{permissionId}")
    @SaAdminCheckPermission("admin:permission:delete")
    public Result<String> deletePermission(@PathVariable Long permissionId) {
        DeletePermissionOperateDTO deletePermissionOperateDTO = rolePermissionService.deletePermission(permissionId);
        return Result.ok("删除账号权限成功");
    }
    /**
     * 在给角色授权时，删除指定角色绑定的指定权限
     * @param roleId
     * @param permissionId
     * @return
     */
    @DeleteMapping("/{roleId}/{permissionId}")
    public Result deleteRolePermission(@PathVariable("roleId") long roleId,
                                       @PathVariable("permissionId")long permissionId){
        DeleteRolePermissionOperateDTO deleteRolePermissionOperateDTO =
                rolePermissionService.deleteRolePermission(roleId, permissionId);
        return Result.ok();
    }


}
